import Qs from 'qs';

export const fmtMicrometer = (value: number | string) => value.toString().replace(/\d+/, n => n.replace(/(\d)(?=(\d{3})+$)/g, $1 => `${$1},`));


export const fmtDeleteString = (val: number | string, delString: string) => {
  const reg = new RegExp(delString, 'g');
  return val.toString().replace(reg, '');
};

export const GetQueryString = (name: string) => {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
  const r = window.location.search.substr(1).match(reg);
  if (r != null) return decodeURI(r[2]);
  return null;
};

export function getQuery(search: string = location.search) {
  if (search.startsWith('?')) search = search.slice(1);
  return Qs.parse(search);
}

export function getQueryValue(
  name: string[],
  search: string = location.search,
) {
  const query = getQuery(search);
  return name.map((n: string) => query[n]);
}
export function getUserCode(): string {
  const userCode = getQueryValue(['userid'])[0] as string;
  if (userCode) return userCode;
  return '';
}

export function verifyUser() {
  const userCode = getUserCode();
  if (!userCode) {
    // eslint-disable-next-line
    alert('抱歉， 未获取到用户账号');
    return false;
  }
  return userCode;
}
export function getMsgId() {
  const userCode = getQueryValue(['msgId'])[0] as string;
  if (userCode) return userCode;
  return '';
}

export const weekFormat = (week: number) => {
  let str;
  if (week === 0) {
    str = '星期日';
  } else if (week === 1) {
    str = '星期一';
  } else if (week === 2) {
    str = '星期二';
  } else if (week === 3) {
    str = '星期三';
  } else if (week === 4) {
    str = '星期四';
  } else if (week === 5) {
    str = '星期五';
  } else if (week === 6) {
    str = '星期六';
  }
  return str;
};


export default {
  fmtMicrometer,
  GetQueryString,
  fmtDeleteString,
};
